#include "Logger.h"
#include <comdef.h>

Logger::Logger(void) {
}

Logger::~Logger(void) {
}

int Logger::log(const char* format, ...) {
	int written = 0;
#ifdef DEBUG
	va_list args;
	va_start(args,format);
	written = log(format,args);
	va_end(args);
#endif
	return written;
}

int Logger::log(const char* format,va_list args) {
	int written = 0;
#ifdef DEBUG
	static char buffer[1024];
	static SYSTEMTIME now; 
	GetSystemTime(&now);
	static char tb[60];
	sprintf_s(tb,"%02d:%02d:%02d:%03d ",now.wHour,now.wMinute,now.wSecond,now.wMilliseconds);
	written = vsnprintf_s(buffer,1023,1024,format,args);
	buffer[1023] = '\0';
	std::string tmsg(tb);
	std::string msg(buffer);
	tmsg.append(msg);
	tmsg.append("\n");
	OutputDebugStringA(tmsg.c_str());
#endif
	return written;
}

int Logger::error(const char* format, ...) {
	int written = 0;
#ifdef DEBUG
	va_list args;
	va_start(args,format);
	written = error(format,args);
	va_end(args);
#endif
	return written;
}

int Logger::error(const char* format,va_list args) {
	int written = 0;
#ifdef DEBUG
	static char buffer[1024];
	written = vsnprintf_s(buffer,1023,1024,format,args);
	buffer[1023] = '\0';
	std::string msg(buffer);
	msg = "[ERROR] : "+msg+"\n";
	OutputDebugStringA(msg.c_str());
#endif
	return written;
}

int Logger::HRError(std::string msg,HRESULT hr) {
	OutputDebugStringA(msg.c_str());
	OutputDebugString(_com_error(hr).ErrorMessage());
	OutputDebugStringA("\n");
	return 0;
}